home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 August / August CD.bin / Shareware / Education / numericalmethods Folder / chap_10 / forwdif.m < prev    next >
Encoding:
Text File  |  1994-06-05  |  762 b   |  30 lines  |  [MATF/MATL]

  1. function U = forwdif(f,g1,g2,a,b,c,n,m)
  2. % U = forwdif(f,g1,g2,a,b,c,n,m)
  3. % Finite difference solution to the heat equation.
  4. % f  is a function, input.
  5. % g1 is a function, input.
  6. % g2 is a function, input.
  7. % a  is the width of [0 a], input.
  8. % b  is the width of [0 b], input.
  9. % c  is the constant in the heat equation, input.
  10. % n  is the number of grid points over [0 a], input.
  11. % m  is the number of grid points over [0 b], input.
  12. % U  is the solution matrix, output.
  13. h = a/(n-1);
  14. k = b/(m-1);
  15. r = c^2*k/h^2;
  16. s = 1 - 2*r;
  17. U = zeros(n,m);
  18. for j = 1:m,
  19.      U(1,j) = feval(g1,k*(j-1));
  20.      U(n,j) = feval(g2,k*(j-1));
  21. end
  22. for i = 2:(n-1),
  23.      U(i,1) = feval(f,h*(i-1));
  24. end
  25. for j = 2:m,
  26.   for i = 2:(n-1),
  27.        U(i,j) = s*U(i,j-1) + r*(U(i-1,j-1) + U(i+1,j-1));
  28.   end
  29. end
  30.